package edu.gatech.cs2340.tigershroud;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
/**
 * RegisterActivity allows the user to enter input
 * @author [Insert Name]
 * @version 9.0x, Build April 3, 2012.
 */
public class RegisterActivity extends Activity 
{   //instance variables
	private UserDataSource ds;
	private Button create, cancel;
	private EditText uname, passw, cpassw, name, email, ques, answ;
	private String suname, spassw, scpassw, sname, semail, sques, sansw;

	/**
	 * The onCreate method is a default android method that is called when the
	 * activity is first started. This onCreate method assigns passed objects to variables, determines the layout of
	 * the activity, and sets listeners for items within the activity
	 * @param saveInstanceState the Bundle name value pair that represents the state of the object
	 *
	 */
    @Override
	public void onCreate(Bundle savedInstanceState) 
    {
    	//default call to super
        super.onCreate(savedInstanceState);
        //set view with register.xml
        setContentView(R.layout.register);
        
        //intialize ds to refer to the over-arching database and open it
        ds = new UserDataSource(this);
		ds.open();
        
		Log.d("REG", "inside register");
		
		//assign layout to buttons 
        create = (Button) findViewById(R.id.bcreate);
        cancel = (Button) findViewById(R.id.bcancel);
        
		uname = (EditText)findViewById(R.id.etuname);
    	passw = (EditText)findViewById(R.id.etpass);
    	cpassw = (EditText)findViewById(R.id.etcpass);
    	name = (EditText)findViewById(R.id.etname);
    	email = (EditText)findViewById(R.id.etemail);
    	ques = (EditText)findViewById(R.id.etques);
    	answ = (EditText)findViewById(R.id.etansw);
        
        suname = "";
        spassw = "";
        scpassw = "";
        sname = "";
        semail = "";
        sques = "";
        sansw = "";
        
        //sets behavior for when create is clicked
        create.setOnClickListener(new View.OnClickListener() 
        {
        	
        	/**
        	 * This method inside the anonymous inner-class OnClickListener
        	 * determines the behavior of the application when the item is clicked.
        	 * When the item is clicked, the text fields are initialized, the entered strings
        	 * are checked against the database, and the application attempts to add a new
        	 * user to the database
        	 * @param v the View that is generated by the parent activity
        	 */
        	public void onClick(View v) 
        	{
	        	suname = uname.getText().toString();
	        	spassw = passw.getText().toString();
	        	scpassw = cpassw.getText().toString();
	        	sname = name.getText().toString();
	        	semail = email.getText().toString();
	        	sques = ques.getText().toString();
	        	sansw = answ.getText().toString();

	        	Log.d("REG", "clicked create");
	        	
	        	//if the username is already in database
	        	if (ds.findUser(suname) != null) 
	        	{
	            	Log.d("REG", "user already exist");
	        		Toast.makeText(getApplicationContext(), "User already exists, please try again...", Toast.LENGTH_LONG).show();
	        		ds.close();
	        	}
	        	else 
	        	{
	        		Log.d("REG", "new user created");

	        		if(validateUsername(suname) && validatePassword(spassw, scpassw) 
	        				&& validateEmail(semail) && validateName(sname) 
	        				&& validateQuestion(sques) && validateAnswer(sansw))
	        		{
		        		ds.createUser(suname, spassw, sname, semail, "normal", sques, sansw);
		        		ds.close();
		        		Toast.makeText(getApplicationContext(), "Successfully created a new user...", Toast.LENGTH_LONG).show();
		        		finish();
	        		}
	        	}
        	}
        });
        
        //sets the behavior for the cancel button
        cancel.setOnClickListener(new View.OnClickListener() 
        {	
        	/**
        	 * This method inside the anonymous inner-class OnClickListener
        	 * determines the behavior of the application when the item is clicked.
        	 * When the item is clicked, the user cancels the current activity
        	 * and returns to the previous activity
        	 * @param v the View that is generated by the parent activity
        	 */
        	public void onClick(View v) 
        	{
            	Log.d("REG", "clicked cancel, go back to login");
            	Toast.makeText(getApplicationContext(), 
            			"User creation cancelled...", Toast.LENGTH_LONG).show();
        		ds.close();
        		finish();
        	}
        });
    }
    
    public boolean validatePassword(String pass, String cpass)
    {
    	if(emptyInputValidation(pass, "a password"))
    	{
    		if((emptyInputValidation(cpass, "the password confirmation")))
    		{
    			if(pass.equals(cpass))
    			{
    				return true;
    			}
    			Toast.makeText(getApplicationContext(), 
        				"Password confirmation failed, please check password inputs...", Toast.LENGTH_LONG).show();
    		}
    	}
    	return false;
    }
    
    public boolean validateEmail(String email)
    {
    	if(emptyInputValidation(email, "an email"))
    	{
        	if(email.indexOf('@') == -1)
        	{
        		Toast.makeText(getApplicationContext(), 
            			"Email validation failed, please enter a valid email address...", Toast.LENGTH_LONG).show();
        		return false;
        	}
        	return true;
    	}
    	return false;
    }
    
    public boolean validateQuestion(String question)
    {
    	return emptyInputValidation(question, "a security question");
    }
    
    public boolean validateAnswer(String answer)
    {
    	return emptyInputValidation(answer, "an answer to the security question");
    }
    
    public boolean validateName(String name)
    {
    	return emptyInputValidation(name, "a name");
    }
    
    public boolean validateUsername(String uname)
    {
    	return emptyInputValidation(uname, "a username");
    }
    
    public boolean emptyInputValidation(String d, String e)
    {
    	if(d.equals(""))
    	{
    		Toast.makeText(getApplicationContext(), 
        			"Please enter " + e + "...", Toast.LENGTH_LONG).show();
    		return false;
    	}
    	return true;
    }
}
